from sc_engine.apps import messages as msgs

from sc_engine.apps.aliasautogen.generator import generate_aliases

def make_app(app_name, root_config, config):
    app = AliasAutogenApp(generate_aliases)

    return {
        msgs.PlayerDataAnnouncement: app.player_data_announcement,
    }

class AliasAutogenApp(object):
    """Alias generator application.

    Listens for player data announcements and generates possible aliases for the
    player by removing common prefixes and suffixes from their name. Will send
    out messages to add these new aliases.
    """

    def __init__(self, alias_generator):
        self.alias_generator = alias_generator

    def player_data_announcement(self, msg):
        known_aliases = [x.lower() for x in msg.aliases]
        all_results = self.alias_generator(msg.name)

        results = [x for x in all_results
            if x.lower() != msg.name.lower() and
                x.lower() not in known_aliases]

        return [msgs.PlayerAliasAddition(msg.player_id, x) for x in results]

